import QtQuick /*2.15*/
import QtQuick.Window /*2.15*/
import QtQuick.Controls/* 2.0*/
import QtQuick.Layouts/* 1.0*/

Window {
    id: root
    width: 640
    height: 480
    visible: true
    title: qsTr("Hello World")

    property int cnt: 0

    Component.onCompleted: {
        console.log("time:" + Qt.formatDateTime(new Date(), "yyyy-MM-dd hh:mm:ss.zzz ddd"))
    }

    ListModel{
        id: typesModel
        Component.onCompleted: {
            typesModel.append({typename:"图片",fileModel:imgModel});
            typesModel.append({typename:"文档",fileModel:docModel});
            typesModel.append({typename:"图片",fileModel:imgModel});
        }
    }

    ListModel{
        id:imgModel
        Component.onCompleted: {
            for(var i = 0; i < 1000; i++){
                imgModel.append({filename:String("hello_%1.jpg").arg(i)});
            }
        }
    }
    ListModel{
        id:docModel
        ListElement{filename:"1.txt";}
        ListElement{filename:"总结报告.doc";}
    }

    ListModel{
        id:selectedModel
    }

    Component{
        id: delegateItem
        Text{
            id: fileText
            height:20
            text: filename

            Component.onCompleted: {
                cnt++;
            }
            Component.onDestruction: {
                cnt--;
            }
        }
    }

    ListView{
        id: typeListView
        anchors.fill: parent
        model: typesModel
//        cacheBuffer: 100
        delegate: Column{
            width: typeListView.width


            Button{
                id: btn
                checkable: true
                width: typeListView.width
                height: 60
                text: String("%1(%2)").arg(typename).arg(fileModel.count)

                Component.onCompleted: btnGp.addButton(btn)
                onVisibleChanged: filesListView.visible = visible
                onCheckedChanged: {
                    console.log("selectedModel count:",selectedModel.count);
                }
            }

            ListView{
                id: filesListView
                width: parent.width
                height: btn.checked ? root.height : 0
                visible: btn.checked
                model: fileModel
                cacheBuffer: filesListView.height * 2

                delegate: delegateItem
                clip: true
                onVisibleChanged: {
                    if(visible)
                        filesListView.height = Math.min(20,fileModel.count) * 20
                    else
                        filesListView.height =  0
                }

                onAddChanged: {
                    console.log("addddddddddd")
                }

                Component.onCompleted: {
                    console.log("onCompleted----------------index:",index);
                    selectedModel.set(index,{typename:String("name:%1").arg(index)})
                }
                Component.onDestruction: {
                    console.log("onDestruction----------------");
                }
            }
        }
    }

    Text{
        color: "red"
        anchors.right: parent.right
        text:String("创建总数:%1").arg(cnt)
    }
    ButtonGroup{
        id: btnGp
        exclusive: false
    }
}
